package com.caigou.component;

import com.caigou.bean.cms.entity.Article;
import com.caigou.mapper.cms.ArticleMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.io.File;
import java.text.SimpleDateFormat;

@Slf4j
@Component
public class DeleteFileComponent {

    @Value("${webRoot}")
    private String webRoot;
    @Value("${webRoot2}")
    private String webRoot2;

    @Resource
    private ArticleMapper articleMapper;

    public void deleteFile(String articleId){
        // 获取文章数据信息
        Article article = articleMapper.loadArticle(articleId);
        if (null == article) return;
        SimpleDateFormat sdf1 = new SimpleDateFormat("yyyyMMdd");
        String hashUrl1 = sdf1.format(article.getCreateTime());

        String floder= "";
        // 判别内容字段--这个部分集合了文章底层页中各种类型文章的独有数据
        switch (article.getArticleType()) {// 判别正文显示类型/模板调用类型
            case 2:// 法律法规
                floder = "/web/law/";
                break;
            case 3:// 案例点评
                floder = "/web/case/";
                break;
            case 4:// 图集资讯
                floder = "/web/album/";
                break;
            case 5:// 政采周报(音频)
            case 6:// 亚利聊政采
                floder = "/web/audio/";
                break;
            case 14:// 专题
                floder = "/web/special/";
                break;
            default:
                floder = "/web/news/";
        }

        // 删除主文件
        File file1 = new File(webRoot2+"caigou-web"+floder+hashUrl1+"/"+articleId+".html");
        if (file1.isFile() && file1.exists()) file1.delete();
        // 汽车
        File file2 = new File(webRoot2+"caigou-qiche"+floder+hashUrl1+"/"+articleId+".html");
        if (file2.isFile() && file2.exists()) file2.delete();
        // IT
        File file3 = new File(webRoot2+"caigou-it"+floder+hashUrl1+"/"+articleId+".html");
        if (file3.isFile() && file3.exists()) file3.delete();
        // 电梯
        File file4 = new File(webRoot2+"caigou-dianti"+floder+hashUrl1+"/"+articleId+".html");
        if (file4.isFile() && file4.exists()) file4.delete();
        // 家具
        File file5 = new File(webRoot2+"caigou-jiaju"+floder+hashUrl1+"/"+articleId+".html");
        if (file5.isFile() && file5.exists()) file5.delete();
        // 空调
        File file6 = new File(webRoot2+"caigou-kongtiao"+floder+hashUrl1+"/"+articleId+".html");
        if (file6.isFile() && file6.exists()) file6.delete();
        // 军队
        File file7 = new File(webRoot2+"caigou-jundui"+floder+hashUrl1+"/"+articleId+".html");
        if (file7.isFile() && file7.exists()) file7.delete();
    }

    public void deleteArticleShareImage(String articleId) {
        // 获取文章数据信息
        Article article = articleMapper.loadArticle(articleId);
        if (null == article) return;
        SimpleDateFormat sdf1 = new SimpleDateFormat("yyyyMMdd");
        File file = new File(webRoot+"/web/"+sdf1.format(article.getCreateTime())+"/"+articleId+".png");
        if (file.isFile() && file.exists()){
            log.info("删除单个文件"+articleId+"成功！");
            file.delete();
        } else {
            log.info("删除单个文件"+articleId+"失败！");
        }
    }
}
